//
//  GoodTypesCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/22.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "GoodTypesCell.h"

#import "HeaderEntryCell.h"

@interface GoodTypesCell()

@property (nonatomic,strong) UICollectionView *collectionV;

@end


@implementation GoodTypesCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
                
        [self initUI];
    }
    return self;
}

- (void)setSourceArr:(NSMutableArray *)sourceArr{
    _sourceArr = sourceArr;
    [self.collectionV reloadData];
}

-(void)initUI{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth-20)/5-10, 60);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 0, ScreenWidth-20, 60) collectionViewLayout:layout];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    self.collectionV.backgroundColor = UIColor.whiteColor;
    self.collectionV.showsVerticalScrollIndicator = NO;
    self.collectionV.showsHorizontalScrollIndicator = NO;
    [self.contentView addSubview:self.collectionV];
    [self.collectionV registerClass:[HeaderEntryCell class] forCellWithReuseIdentifier:@"HeaderEntryCell"];
}
#pragma mark - UICollectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((ScreenWidth-20)/5-10, 60);
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.sourceArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MainPageHeaderModel *model = self.sourceArr[indexPath.row];
    static NSString * CellIdentifier = @"HeaderEntryCell";
    HeaderEntryCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.touchBlock) {
        self.touchBlock(indexPath.row);
    }
}


@end
